/**
 * Created by ebalmes Oct 6, 2009 8:32:39 AM
 * 
 */
package gui.dialogs;

import gui.actions.ActionFactory;
import gui.actions.XAction;
import gui.components.XField;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;

public abstract class BasicDialog extends JDialog implements FocusListener, KeyListener
{
  protected static int DEFAULT_WIDTH = 500;
  protected static int DEFAULT_HEIGHT = 300;
  protected JPanel mainPanel;
  JPanel controlPanel;
  /**
   * Listaba tenni a fieldeket?
   */
  protected Map<String, XField> fieldMap;
  protected XAction[] actions;

  public BasicDialog(String title, JFrame owner) {
    super();
    super.setTitle(title);
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    setMinimumSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
    fieldMap = new HashMap<String, XField>();
    createGUI();
    addFocusListeners();
    setModal(true);
    setLocationRelativeTo(owner);
  }

  public abstract void loadContent();

  public abstract void releaseResources();

  public abstract void createGUI();

  protected JPanel createControlPanel(BasicDialog dialog) {
    actions = new XAction[3];
    actions[0] = ActionFactory.getDialogOkAction(dialog);
    actions[1] = ActionFactory.getCloseDialogAction(dialog);
    actions[2] = ActionFactory.getHelpAction();
    JPanel controlPanel = new JPanel(new FlowLayout());
    for(Action a : actions) {
      controlPanel.add(new JButton(a));
    }
    controlPanel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
    actions[0].setEnabled(false);
    return controlPanel;
  }

  public abstract void OK();

  public void setVisible(boolean visibility) {
    if (!visibility) {
      releaseResources();
    } else {
      loadContent();
    }
    super.setVisible(visibility);
  }

  protected boolean validateAllFields() {
    for(XField field : fieldMap.values()) {
      if (!field.validateField()) {
        enabLeOKButton(false);
        return false;
      }
    }
    enabLeOKButton(true);
    return true;
  }

  protected void enabLeOKButton(boolean enable) {
    actions[0].setEnabled(enable);
  }

  protected void addFocusListeners() {
    for(XField field : fieldMap.values()) {
      field.addFocusListener(this);
    }
  }

  @Override
  public void focusGained(FocusEvent e) {
    // TODO Auto-generated method stub
  }

  @Override
  public void focusLost(FocusEvent e) {
    validateAllFields();
  }
  
  @Override
  public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub
    
  }
  
}
